package com.semidata.trp.material.http;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;

import com.semidata.rtd.core.http.server.AbstractDispatchHandler;
import com.semidata.rtd.core.http.server.HttpSession;
import com.semidata.trp.material.service.MaterialService;

public class MaterialUploadHttpHandler extends AbstractDispatchHandler {

    private String type;

    public MaterialUploadHttpHandler(ChannelHandlerContext ctx, HttpRequest request, String type) {
        super(ctx, request);
        this.type = type;
    }

    @Override
    public void handle(HttpSession session) {
        ByteBuf buffer = session.getBuffer();
        int size = buffer.readableBytes();
        byte[] bytes = new byte[size];
        buffer.readBytes(bytes);
        Object ret = MaterialService.upload(bytes, type);
        session.getResponse().end(ret.toString());
        
    }

}
